package com.ch.web.controller;

import com.ch.web.ResponseDto.xyTalk.TalkResponseDto;
import com.ch.web.entity.user.XyTalkEntity;
import com.ch.web.requestDto.xyTalk.request.XyTalkAddRequest;
import com.ch.web.service.XyTalkService;
import com.ch.web.utils.PageUtils;
import com.ch.web.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.constraints.NotNull;
import java.util.Arrays;
import java.util.List;
import java.util.Map;


/**
 * @author chenHao
 * @email 1429542285@qq.com
 * @date 2022-10-15 13:58:33
 */
@RestController
@RequestMapping("xy/talk")
public class XyTalkController {
    @Autowired
    private XyTalkService xyTalkService;

    /**
     * 列表
     */
    @PostMapping("/list")
    public R list(@RequestBody Map<String, Object> map, HttpServletRequest request) {
        List<TalkResponseDto> responseDtos = xyTalkService.queryPage(map, request);
        if (responseDtos.size() > 0) {
            return R.ok().put("talkList", responseDtos);
        } else {
            System.out.println("无数据");
            return R.error();
        }
    }

    /**
     * 列表
     */
    @PostMapping("/adminGetList")
    public R adminGetList(@RequestBody Map<String, Object> map, HttpServletRequest request) {
        PageUtils page= xyTalkService.adminGetList(map, request);

        return R.ok().put("page", page);
    }

    /**
     * 信息
     */
    @RequestMapping("/info/{id}")

    public R info(@PathVariable("id") Long id) {
        XyTalkEntity xyTalk = xyTalkService.getById(id);

        return R.ok().put("xyTalk", xyTalk);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")

    public R save(@RequestBody XyTalkEntity xyTalk) {
        xyTalkService.save(xyTalk);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")

    public R update(@RequestBody XyTalkEntity xyTalk) {
        xyTalkService.updateById(xyTalk);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] ids) {
        xyTalkService.removeByIds(Arrays.asList(ids));
        return R.ok();
    }

    /**
     * 发表说说
     */
    @RequestMapping("/shareTalk")
    public R add(@RequestBody XyTalkAddRequest addRequest, HttpServletRequest request) {
        xyTalkService.addShareTalk(addRequest, request);
        return R.ok();
    }

    /**
     * 获取自己的说说
     */
    @RequestMapping(value = "/getUserTalkList", method = RequestMethod.POST)
    public R getUserTalkList(@RequestBody Map<String, Object> map, HttpServletRequest request) {
        return R.ok().put("pageData", xyTalkService.getUserTalkList(map, request));
    }

    /**
     * 根据talkId获取指定的说说
     */
    @GetMapping("/selfTalk")
    public R selfTalk(@RequestParam("talkId") Long talkId, HttpServletRequest request) {
        System.out.println(talkId);
        return R.ok().put("talkList", xyTalkService.getSelfTalk(talkId, request));
    }

    /**
     * 删除
     */
    @RequestMapping("/selfDeleteTalkById")
    public R selfDeleteById(@NotNull @RequestBody Long id) {
        Boolean b = xyTalkService.selfDeleteTalkById(id);
        return R.okOrError(b);
    }
}
